﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Winz.HBRelog;
using Microsoft.Win32.TaskScheduler;


namespace Winz
{
    class VarCorrecter
    {
        static void Main(string[] args)
        {

            // Get the service on the local machine
            using (TaskService ts = new TaskService())
            {
                Task t = ts.FindTask("WinzFileCorrecter");
                if (t == null)
                {
                    // Create a new task definition and assign properties
                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = "Launch Winz File Converter";

                    // Create a trigger that will fire the task at this time every other day
                    TimeTrigger tt = new TimeTrigger();
                    tt.Repetition.Interval = TimeSpan.FromMinutes(60);
                    td.Triggers.Add(tt);
                    // Create an action that will launch Notepad whenever the trigger fires
                    td.Actions.Add(new ExecAction(Process.GetCurrentProcess().MainModule.FileName, null, null));
                    // Register the task in the root folder
                    ts.RootFolder.RegisterTaskDefinition(@"WinzFileCorrecter", td);
                }
            }


            List<LuaChecker> checkers = new List<LuaChecker>();
            HBRelogHelper HBR = new HBRelogHelper();
            //HBR.Pause();
            //HBR.Restart();

            Settings s = Settings.Load();
            s.Accounts.ForEach(a => checkers.Add(new LuaChecker(s.wowPath,a)));

            Log.Debug("{0} ", s.wowPath); 
            foreach (string t in s.Accounts)
            {
                Log.Debug("   {0} ", t);
            }
            
            if(checkers.Any(c => !c.Check() )) {
                HBR.Pause();
            }
            checkers.ForEach(c => c.Correct());
            HBR.Restart();

            /*
            s.wowPath = @"C:\WoW";
            s.Accounts = new List<string>(new string[] { "124906523#1", "PSYCHOSEB" });
            s.Save();
            /*

            list.Add(new LuaChecker(@"C:\WoW", "124906523#1"));
            list.Add(new LuaChecker(@"C:\WoW", "PSYCHOSEB"));

            foreach(LuaChecker t in list)
                if (!t.Check())
                {
                    HBR.Pause();
                    t.Correct();
                }
            HBR.Restart();


            //Console.WriteLine(string.Format("{0} {1}", t.accountPath, t.Check()));

            /*
            
            Lua lua = new Lua();
            LuaTable t;
            lua.DoFile("C:\\WoW\\WTF\\Account\\124906523#1\\SavedVariables\\TradeSkillMaster_Crafting.lua");
            t = lua.GetTable("TradeSkillMaster_CraftingDB.profiles.Default.Enchanting.crafts");
            

            int count = 0;
            foreach(DictionaryEntry member in t) {
                count++;
                Console.WriteLine("({0}) {1} = ({2}) {3}",
                  member.Value.GetType().ToString(),
                  member.Key,
                  member.Value.GetType().ToString(),
                  member.Value);
            }

            

            Console.WriteLine("count : " + count);
            */

        }
    }
}
